/**
 * Rook.
 * 
 * @author Brian Boadi, Antoine Domenger
 * @version 1.0
 */


package lp.chess;

public class Rook extends Piece {
	//- CONSTRUCTOR(S) ------------------------------------------------------------------------

	/**
 	 * Default constructor.
 	 * Initializes an empty rook.
 	 */
	public Rook() {
		super();
	}

	/**
	 * Initializes a new rook of a given color.
	 * 
	 * @param white true if the rook must be white, false if it must be black.
	 */
	public Rook(boolean white) {
		super(white);
	}
	
	/**
	 * Initializes a new rook with all of its attributes.
	 * 
	 * @param x	the x coordinate of the rook on the chessboard.
	 * @param y the y coordinate of the rook on the chessboard.
	 * @param white true if the rook must be white, false if it must be black.
	 */
	public Rook(int x, int y, boolean white) {
		super(x, y, white);
	}
	
	/**
	 * Copy constructor.
	 * Initializes a new rook from a given one.
	 * 
	 * @param other the rook to copy in the new one.
	 */
	public Rook(Rook other) {
		super(other);
	}
	
	//-----------------------------------------------------------------------------------------

	//- OTHER METHOD(S) -----------------------------------------------------------------------

	/**
	 * Checks if a move is valid for this kind of piece.
	 * 
	 * @param cb the chessboard.
	 * @param x	the x coordinate where we want the rook to move on the chessboard.
	 * @param y the y coordinate where we want the rook to move on the chessboard.
	 * @param act true if we want to remove the piece that can be taken.
	 * 
	 * @return true if the move is valid.
	 */
	public boolean validMove(Chessboard cb, int x, int y, boolean act)
	{
		boolean isValid = (cb.isOutOfRange(x, y) && differentPosition(x, y));
		int i;
		
		// Specific to this piece :
		if(isValid && !(
				(x == getX() && (y > getY() || y < getY())) ||
				(y == getY() && (x > getX() || x < getX()))
				)) {
			isValid = false;
		}
		
		// This will check if the path is clear:
		if(x == getX()) {	// The piece tries to move vertically.
			if(y > getY()) {
				i = getY() + 1;
				
				while(i < y && isValid) {
					if(cb.getPiece(x, i) != null) isValid = false;
					i++;
				}
			}
			else if(y < getY()) {
				i = getY() - 1;
				
				while(i > y && isValid) {
					if(cb.getPiece(x, i) != null) isValid = false;
					i--;
				}
			}
		} else if (y == getY()) {	// The piece tries to move horizontally.
			if(x > getX()) {
				i = getX() + 1;
				
				while(i < x && isValid) {
					if(cb.getPiece(i, y) != null) isValid = false;
					i++;
				}
			}
			else if(x < getX()) {
				i = getX() - 1;
				
				while(i > x && isValid) {
					if(cb.getPiece(i, y) != null) isValid = false;
					i--;
				}
			}
		}
		
		if(isValid && cb.getPiece(x, y) != null && act)	// If there is already a piece here, it can now be taken:
		{
			if(cb.getPiece(x, y).isWhite() != isWhite()) cb.removePiece(x, y);	// The piece can be taken.
			else isValid = false;
		}
		
		return isValid;
	}

	/**
	 * Returns the character associated to the type of this piece.
	 * 
	 * @return the character associated to the type of this piece 
	 */
	public char getType()
	{
		if(isWhite()) return 'R';
		else return 'r';
	}
	
	/**
	 * Returns a copy of this piece.
	 * 
	 * @return a copy of this piece.
	 */
	public Piece getCopy() {
		Rook copy = new Rook(m_x, m_y, m_white);
		
		return copy;
	}
	
	//-----------------------------------------------------------------------------------------
}
